package com.yang.controller.admin;

import com.yang.bean.AdminInfoBean;
import com.yang.bean.LoginUserData;
import com.yang.bean.MarketAdmin;
import com.yang.bean.common.BaseRespVo;
import com.yang.bean.yvo.SelectUserPermsBean;
import com.yang.config.realm.MarketToken;
import com.yang.exception.WXException;
import com.yang.service.admin.AdminAuthService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("admin/auth")
public class AuthController {


    @Autowired
    AdminAuthService adminAuthService;

    @Autowired
    DefaultWebSessionManager defaultWebSessionManager;
    //TODO 思路可以在不同的service中获取相同session，而不是在contrller中获取

    /**
     * @description: 登录认证
     * [map]
     * @return: com.yang.bean.common.BaseRespVo
     * @author: 杨庆
     * @time: 2022.9.8 17:26
     */
    @PostMapping("login")
    public BaseRespVo login(@RequestBody Map map) {
        String username = (String) map.get("username");
        String password = (String) map.get("password");
        Subject subject = SecurityUtils.getSubject();
        // 自定义的
        MarketToken usernamePasswordToken = new MarketToken(username, password, "admin");
        try {
            subject.login(usernamePasswordToken);
        } catch (Exception e) {
            e.printStackTrace();
        }
        boolean authenticated = subject.isAuthenticated();
        if (!authenticated) {
            return BaseRespVo.invalidParameter("用户名密码错误");
        }
        Session session = subject.getSession();
        String sid = (String) session.getId();
        MarketAdmin marketAdmin = (MarketAdmin) subject.getPrincipals().getPrimaryPrincipal();
        LoginUserData loginUserData = new LoginUserData();
        AdminInfoBean adminInfo = new AdminInfoBean();
        adminInfo.setAvatar(marketAdmin.getAvatar());
        adminInfo.setNickName(marketAdmin.getUsername());
        loginUserData.setAdminInfo(adminInfo);
        loginUserData.setToken(sid);
        session.setAttribute(sid, marketAdmin);
        return BaseRespVo.ok(loginUserData);
    }

    @RequestMapping("info")
    public BaseRespVo info(String token) {
        //根据token就是登录的时候设置的sessionid 根据token获取用户信息
        MarketAdmin marketAdmin = null;
        try {
            Session session = defaultWebSessionManager.getSession(() -> token);
            marketAdmin = (MarketAdmin) session.getAttribute(token);
        } catch (Exception e) {
            return BaseRespVo.error("验证失效，请重新登录");
        }
        //TODO 根据当前的用户去查询数据
        SelectUserPermsBean selectUserPermsBean = adminAuthService.selectUserPerms(marketAdmin);
        return BaseRespVo.ok(selectUserPermsBean);
    }

    @RequestMapping("logout")
    public BaseRespVo logout() {
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return BaseRespVo.ok();
    }
}